package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.traval.biz.StrategyBiz;
import com.etc.traval.biz.StrategycomBiz;
import com.etc.traval.biz.UserBiz;
import com.etc.traval.bizimpl.StrategyBizImpl;
import com.etc.traval.bizimpl.StrategycomBizImpl;
import com.etc.traval.bizimpl.UserBizImpl;
import com.etc.traval.util.MyUtil;
import com.etc.travel.entity.Strategy;
import com.etc.travel.entity.Strategycom;
import com.etc.travel.entity.User;
import com.google.gson.Gson;

public class StrategycomServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public StrategycomServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		StrategycomBiz sbiz=new  StrategycomBizImpl();
		String flag=request.getParameter("flag");
		if("query".equals(flag)){	
	    int id=Integer.parseInt(request.getParameter("id"));
			
			
		List<Strategycom> list=sbiz.queryallStrategycom(Integer.parseInt(request.getParameter("id")), Integer.parseInt(request.getParameter("page")), Integer.parseInt(request.getParameter("size")));
		Gson gson=new Gson();
		String gsonString =gson.toJson(list);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.print(gsonString );
		out.flush();
		out.close();
		}else if("add".equals(flag)){
			UserBiz ubiz=new UserBizImpl();
			StrategyBiz stbiz=new StrategyBizImpl();
			int id=Integer.parseInt(request.getParameter("user"));
			String content=request.getParameter("content");
			int strate=Integer.parseInt(request.getParameter("strategy"));
			Strategycom com=new Strategycom();
			com.setStrategycom_content(content);
			com.setStrategycom_time(MyUtil.getWebsiteDatetime());
			com.setStrategycom_user(ubiz.queryUserById(id));
			com.setStrategycom_strategy(stbiz.querySTrategyById(strate));
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			if(sbiz.addStrategycom(com)){				
				out.print("添加成功");
			}else{
				out.print("添加失败");		
			}
			out.flush();
			out.close();
		}
		
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
